﻿using System;
using System.Data;
using System.Web;
using System.Web.UI.HtmlControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using log4net;
using System.Text;
using System.Text.RegularExpressions;
using System.Reflection;
using Microsoft.ApplicationBlocks.Data;
using AjaxPro;


namespace AnyOffice.general.status_bar
{
    public partial class sms_show :  BasePage
    {       
 
        private string GetContent(string strContent)
        {
            strContent = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.IgnoreCase).Replace(strContent, "<a href=$1 target=_blank>$1</a>");
            strContent = new Regex(@"([a-zA-Z_0-9.-]+\@[a-zA-Z_0-9.-]+\.\w+)", RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | RegexOptions.IgnoreCase).Replace(strContent, "<a href=mailto:$1>$1</a>");
            return strContent.Replace("\n", "<br>");
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.AppendHeader("Expires", DateTime.Now.AddDays(-1.0).ToString("r"));
            Response.AppendHeader("Pragma", "no-cache");
            Response.AppendHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
            StringBuilder builder = new StringBuilder();
            int newTotalNum = 0;
            DataTable table = new SmsDao().SmsShow(base.Conn, base.User.Identity.Name, ref newTotalNum);
            if (newTotalNum == 0)
            {
                Response.Write(base.ShowMessage("无短信或已删除！"));
                builder.Append("<script Language=\"JavaScript\">\n");
                builder.Append("if(window.opener && !window.opener.closed)\n");
                builder.Append("\tparent.opener.document.getElementById(\"new_sms\").innerHTML=\"\";\n");
                builder.Append("self.close();\n");
                builder.Append("</script>\n");
                base.CloseConn();
                Response.Write(builder.ToString());
                Response.End();
            }
            else
            {
                this.litNewTotalNum.Text = newTotalNum.ToString();
                if (table.Rows.Count > 0)
                {
                    if (Convert.ToInt32(table.Rows[0]["Type"].ToString()) == 0)
                    {
                        this.imgAvatar.ImageUrl = "/images/avatar/" + table.Rows[0]["Avatar"].ToString() + ".gif";
                    }
                    else
                    {
                        this.imgAvatar.ImageUrl = "/images/sms_type" + table.Rows[0]["Type"].ToString() + ".gif";
                    }
                    this.litSmsType.Text = table.Rows[0]["CodeName"].ToString();
                    this.litFromUserName.Text = table.Rows[0]["UserName"].ToString();
                    this.litSendTime.Text = table.Rows[0]["SendTime"].ToString();
                    if (table.Rows[0]["Content"] != DBNull.Value)
                    {
                        this.litContent.Text = this.GetContent(table.Rows[0]["Content"].ToString());
                    }
                    this.ViewState["Type"] = table.Rows[0]["Type"].ToString();
                    this.ViewState["DeptName"] = table.Rows[0]["DeptName"].ToString();
                    this.ViewState["SmsId"] = table.Rows[0]["SmsId"].ToString();
                    this.ViewState["ToId"] = table.Rows[0]["FromId"].ToString();
                    this.ViewState["ToName"] = table.Rows[0]["UserName"].ToString();
                    if (table.Rows[0]["Url"] != DBNull.Value)
                    {
                        this.litLinkButton.Text = "<a href=\"javascript:gotoURL('" + table.Rows[0]["Url"].ToString() + "','" + table.Rows[0]["SmsId"].ToString() + "');\" title=\"到相应的模块去\"> 链接</a>&nbsp;&nbsp;";
                    }
                    else if (table.Rows[0]["FuncCode"] != DBNull.Value)
                    {
                        this.litLinkButton.Text = "<a href=\"javascript:gotoURL('/general/" + table.Rows[0]["FuncCode"].ToString()+"/index.aspx" + "','" + table.Rows[0]["SmsId"].ToString() + "');\" title=\"到相应的模块去\"> 链接</a>&nbsp;&nbsp;";
                    }
                    builder.Append("<script Language=\"JavaScript\">\n");
                    builder.Append("function SmsBack()\n");
                    builder.Append("{\n");
                    builder.Append("\tif(window.opener && !window.opener.closed)\n");
                    builder.Append("\t\tparent.opener.document.getElementById(\"new_sms\").innerHTML=\"\";\n");
                    builder.Append("\tlocation='sms_submit.aspx?sms_id=" + table.Rows[0]["SmsId"].ToString() + "&del=0&to_id=" + table.Rows[0]["FromId"].ToString() + "&to_name=" + table.Rows[0]["UserName"].ToString() + "';\n");
                    builder.Append("}\n");
                    builder.Append("</script>\n");
                    if (Convert.ToInt32(table.Rows[0]["Type"].ToString()) == 0)
                    {
                        this.litReplyButton.Text = builder.ToString() + "\n<a title=\"回复短信\"  href=\"javascript:SmsBack();\">回复</a>&nbsp;&nbsp;";
                    }
                }
            }
        } 

    }
}
